﻿using System;

internal class Program
{
    static void Main(string[] args)
    {
        Shape[] shapes = new Shape[10];
        Random rand = new Random();
        double sum = 0; // 定义面积和
        for (int n = 0; n < shapes.Length; n++)
        {
            int index = rand.Next(0, 3);
            shapes[n] = ShapeFactory.GetShape(index); // 随机生成十个形状
            Console.WriteLine(shapes[n].Details());
            sum += shapes[n].Area;
        }
        Console.WriteLine("面积和为：" + sum);
    }
}

public interface Shape
{
    double Area { get; }
    string Details();
 
}

public interface Factory
{
    Shape CreateShape();
}

public class Rectangle : Shape
{
    double length, width;
    public double Area
    {
        get { return length * width; }
    }

    public Rectangle(double length, double width)
    {
        this.length = length;
        this.width = width;
    }

    public string Details()
    {
        return "矩形长：" + length + "，宽：" + width;
    }
}

public class Square : Shape
{
    double side;
    public double Area
    {
        get { return side * side; }
    }

    public Square(double side)
    {
        this.side = side;
    }

    public string Details()
    {
        return "正方形边长：" + side;
    }
}

public class Triangle : Shape
{
    double Base,height;
    public double Area
    {
        get { return Base * height / 2; }
    }

    public Triangle(double Base, double height)
    {
        this.Base = Base;
        this.height = height;
    }

    public string Details()
    {
        return "三角形底：" + Base + "，高：" + height;
    }
}

public class RecFactory : Factory
{
    public Shape CreateShape()
    {
        Random random = new Random();
        int a = random.Next(1, 10);
        int b = random.Next(1, 10);
        return new Rectangle(a,b); // 示例尺寸
    }
}

public class SquFactory : Factory
{
    public Shape CreateShape()
    {
        Random random = new Random();
        int a = random.Next(1, 10);
        return new Square(a); // 示例尺寸
    }
}

public class TriFactory : Factory
{
    public Shape CreateShape()
    {
        Random random = new Random();
        int a= random.Next(1, 10);
        int b = random.Next(1, 10);
        return new Triangle(a, b); // 示例尺寸
    }
}

public class ShapeFactory
{
    private static readonly Factory[] factories = {
        new RecFactory(),
        new SquFactory(),
        new TriFactory()
    };

    public static Shape GetShape(int index)
    {
        if (index >= 0 && index < factories.Length)
        {
            return factories[index].CreateShape();
        }
        throw new ArgumentOutOfRangeException(nameof(index), "Index is out of range.");
    }
}